import * as Api from '@/syApis/syDa/syDa.js'
export default {
	created() {
		var $this = this;
		uni.getSystemInfo({
			success(res) {
				$this.wrapperHeight = res.windowHeight;
				$this.containerHeight = res.windowHeight - 50;
				$this.orderListHeight = res.windowHeight - 100;
				$this.orderLiHeight = res.windowHeight - 100;
			}
		});
		let date = new Date();
		this.startTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
		this.endTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();

		this.getCgOrder();

	},
	computed: {
		tdAllWidth() {
			var tdAllWidth = 0;
			this.tdWidth.forEach(item => {
				tdAllWidth += parseFloat(item);
			});
			tdAllWidth += this.tdWidth.length - 1;
			return tdAllWidth;
		}
	},
	watch: {
		//统计数量
		orderList(oldval, newval) {
			var inPurSaleQty = 0;
			var inPurSaleAmount = 0
			var outPurQty = 0;
			var outPurAmount = 0;
			var inGetQty = 0;
			var ouGetQty = 0;
			var cyQty = 0;
			var cyAmount = 0;
			var _this = this;
			this.orderList.forEach(item => {
				inPurSaleQty += item.real_qty;
				inPurSaleAmount += item.real_Amount;
				outPurQty += item.return_qty;
				outPurAmount += item.return_Amount;
				inGetQty += item.real_qty2;
				ouGetQty += item.return_qty2;
				cyQty += item.Qty;
				cyAmount += item.Amount;
			});
			this.inPurSaleQty = inPurSaleQty;
			this.inPurSaleAmount = inPurSaleAmount;
			this.outPurQty = outPurQty;
			this.outPurAmount = outPurAmount;
			this.inGetQty = inGetQty;
			this.ouGetQty = ouGetQty;
			this.cyQty = cyQty;
			this.cyAmount = cyAmount;
		}
	},
	onLoad(options) {

		//采购订单
		if (options.type == 0) {
			this.OrderTitle = "商品档案";
			this.isSupperPurb = true;
		}
		//销售订单
		else {
			this.OrderTitle = "商品档案";
			this.isSupperPurb = false;
		}
	},
	methods: {
		//排序
		test(selectIndex) {
			var len = this.orderList.length;
			var arr = this.orderList
			if (selectIndex == 12) {
				if (this.paixu01 == false) {
					this.paixu01 = true
					for (var i = 0; i < len; i++) {
						for (var j = 0; j < len - 1 - i; j++) {
							if (parseFloat(arr[j].VipPrice) > parseFloat(arr[j + 1].VipPrice)) { //相邻元素两两对比
								var temp = arr[j + 1];
								//元素交换
								arr[j + 1] = arr[j];
								arr[j] = temp;
							}
						}
					}
				} else {
					this.paixu01 = false
					for (var i = 0; i < len; i++) {
						for (var j = 0; j < len - 1 - i; j++) {
							if (parseFloat(arr[j].VipPrice) < parseFloat(arr[j + 1].VipPrice)) { //相邻元素两两对比
								var temp = arr[j];
								//元素交换
								arr[j] = arr[j + 1];
								arr[j + 1] = temp;
							}
						}
					}
				}
				return arr;
			}

		},

		AddListen() {
			var _this = this;
			this.$eventHub.$on('fire', function(data) {
				_this.GoodsCode = data.GoodsCode;
				_this.GoodsName = data.GoodsName == "" ? "全部商品" : data.GoodsName;
				_this.BarCode = data.BarCode;
				_this.getCgOrder(data);
				_this.$eventHub.$off('fire');
			});
		},
		//查看明细
		lookDetail(type, goodItem) {
			//查看明细
			if (type == 0) {
				uni.navigateTo({
					url: './goodInfoDetail?type=' + 0 + "&goodInfo=" + JSON.stringify(goodItem)
				})
			}
			//新增商品
			else {
				uni.navigateTo({
					url: './goodInfoDetail?type=' + 1
				})
			}
		},
		//删除商品
		Del(GoodsID) {
			var that = this
			Api.showConfirm("是否删除此<商品档案>", function(state) {
				if (state) {
					let data = {
						GoodsID
					}
					Api.DeleteGoodsMessage(data, function(res) {
						if (res.state) {
							if (res.data.ErrorMsg == "获取登录信息失败") {
								// api.showTip()
							} else if (res.data.ErrorMsg != "") {
								Api.showTip(res.data.ErrorMsg)
							} else {
								Api.showTip("删除成功");
								// that.orderList = '';
								let tempList = [];
								that.orderList.forEach((item, index) => {
									if (item.GoodsID != GoodsID) {
										tempList.push(item);
									}
								})
								that.orderList = tempList;

							}
						} else {
							Api.showTip("服务器开小差了");
						}
					})
				} else {
					console.log("点击了取消")
				}
			})
		},
		//修改宽度
		changeWidth() {
			this.showChange = true;
		},
		hideChaneg() {
			this.showChange = false;
		},
		//返回上一页
		back() {
			uni.navigateBack({
				delta: 1
			})
		},
		//跳转到筛选条件页
		filter() {
			this.AddListen();
			uni.navigateTo({
				url: './goodsMessageFilter'
			})
		},

		Add() {
			uni.navigateTo({
				url: "./goodInfoDetail?type=" + 1
			})
		},
		//获取汇总数据
		getCgOrder: function(filter) {
			let data = {};
			if (filter == undefined) {
				data = {
					BillName: this.OrderTitle
				}
			} else {
				data = filter;
			}
			var _this = this;
			if (!this.isSupperPurb) {
				Api.Get_GoodsMessage(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg == "获取登录信息失败") {

						} else if (res.data.ErrorMsg != "") {
							Api.showTip(res.data.ErrorMsg);
						} else {
							_this.orderList = _this.setGetTwoFloat(res.data.ResultMsg);
						}
					} else {
						Api.showTip("服务器开小差了");
					}
				});
			} else {
				Api.GetSupplierPurchase(data, function(res) {
					if (res.state) {
						if (res.data.ErrorMsg == "获取登录信息失败") {

						} else if (res.data.ErrorMsg != "") {
							Api.showTip(res.data.ErrorMsg);
						} else {
							_this.orderList = _this.setGetTwoFloat(res.data.ResultMsg);
						}
					} else {
						Api.showTip("服务器开小差了");
					}
				});
			}
		},
		//将数额保留两位小数
		setGetTwoFloat(arr) {
			arr.forEach((item, index) => {
				arr[index].real_qty = Api.setTwoFloat(item.real_qty);
				arr[index].real_Amount = Api.setTwoFloat(item.real_Amount);
				arr[index].return_qty = Api.setTwoFloat(item.return_qty);
				arr[index].real_qty2 = Api.setTwoFloat(item.real_qty2);
				arr[index].real_qty = Api.setTwoFloat(item.real_qty);
				arr[index].return_qty2 = Api.setTwoFloat(item.return_qty2);
				arr[index].Qty = Api.setTwoFloat(item.Qty);
				arr[index].Amount = Api.setTwoFloat(item.Amount);
			});
			return arr;
		},
		lookMore: function(index) {
			this.nowRote = this.nowRote == index ? "-1" : index;
		},
		changeListAl: function() {
			this.isList = !this.isList;
		},
	},
	onShow() {
		this.$eventHub.$off('fire');
	},
	data() {
		return {
			showChange: false,
			orderList: [],
			containerHeight: 100,
			wrapperHeight: 600,
			orderListHeight: 100,
			orderLiHeight: 100,
			isList: true,
			startTime: "",
			endTime: "",
			curstomName: "全部客户",
			//订单页标题
			OrderTitle: "",
			//是否供应商销售汇总
			isSupperPurb: false,
			nowRote: -1,
			//进货/销售数量总计
			inPurSaleQty: 0,
			//进货/销售金额总计
			inPurSaleAmount: 0,
			//退货数量总计
			outPurQty: 0,
			//退货金额总计
			outPurAmount: 0,
			//进货数总计
			inGetQty: 0,
			//退货数总计
			ouGetQty: 0,
			//差异数
			cyQty: 0,
			//差异金额
			cyAmount: 0,
			//商品ID
			GoodsID: "",
			//td宽度列表
			tdWidth: [125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125, 125],
			//tb表头
			tdHeaderList: [{
					name: "序号"
				},
				{
					name: "商品编号"
				}, {
					name: "商品名称"
				}, {
					name: "类型"
				}, {
					name: "规格"
				}, {
					name: "主条码"
				}, {
					name: "单位"
				}, {
					name: "进价"
				}, {
					name: "零售价"
				}, {
					name: "单价1"
				}, {
					name: "单价2"
				}, {
					name: "配送价"
				}, {
					name: "会员价"
				}, {
					name: "批发价"
				}, {
					name: "停止进货"
				}, {
					name: "停止销售"
				}, {
					name: "停用"
				}, {
					name: "批号管理"
				}, {
					name: "主供应商"
				}
			],
			GoodsCode: "",
			GoodsName: "",
			BarCode: "",
			paixu01: false
		}
	}
}
